<template>
    <TabGrayEasyCard :title="'欠款追缴'" @onMore="goDetail(props.permi)" :tabs="card.tabs" :curTab="card.curTab"
        :tabRight="card.tabRight" @onChange="changeTab" :grayCardList="card.grayCardList" :lineText="''" :barText="''"
        :unitText="``" :updateText="card.updateText"
        v-btnPermi="{ permi: props.permi, noReport: true, isHiden: true, hideList: props.hideList }">
        <slot>
            <el-table :data="card.tableData" height="100%">
                <el-table-column prop="floorName" :label="tableLabel" :align="'left'"  min-width="60"/>
                <el-table-column label="租费欠款" :align="'right'">
                    <template #default="scope">
                        {{ toThousands(scope.row.debtamt) }}
                    </template>
                </el-table-column>
                <el-table-column label="其中:已诉讼" :align="'right'" min-width="100">
                    <template #default="scope">
                        {{ scope.row.islitigation ? toThousands(scope.row.islitigation) : '-'}}
                    </template>
                </el-table-column>
                <el-table-column label="其中:关联方" :align="'right'" min-width="100">
                    <template #default="scope">
                        {{ toThousands(scope.row.isrelationcustomer) }}
                    </template>
                </el-table-column>
            </el-table>
        </slot>
    </TabGrayEasyCard>
</template>

<script setup>
import TabGrayEasyCard from '@/DbComponents/tabGrayEasyCard'
import { toThousands } from '@/utils/number-util'
import dashBoardApi from '@/api/Manage/Business/dashBoard'
import { ref, reactive, watch, computed } from 'vue'
import { reportLog } from '@/utils/reportLog'
import { useRouter } from 'vue-router'
const router = useRouter()

const props = defineProps({
    permi: {
        type: String,
        required: false,
        default: ''
    },
    hideList: {
        type: Array,
        required: false,
    },
    biProjectCode: {
        type: String,
        required: false,
        default: ''
    },
    updateDate: {
        type: String,
        required: false,
        default: ''
    }
})
const card = reactive(
    {
        tabs: [{ label: '全部', value: '全部' },{ label: '当年', value: '当年' }, { label: '历史', value: '历史' }],
        curTab: '全部',
        tabRight: '',
        grayCardList: [{ label: '租费欠款', value: '', unit: '万', color: '' }, { label: '其中:已诉讼', value: '', unit: '万', color: '' }, { label: '其中:关联方', value: '', unit: '万', color: '' }],
        unitText: '万元',
        updateText: '',
        tableData: [],
        startDate: '',
        endDate: '',
    }
)

const getCardData = () => {
    dashBoardApi.queryDeptCardApi({ projectCode: props.biProjectCode, timeType: card.curTab }).then((res) => {
        if (res.data.code == 200) {
            card.tabRight = res.data.data.timeLab
            card.grayCardList[0].value = toThousands(res.data.data.debtamt)
            card.grayCardList[1].value = toThousands(res.data.data.islitigation)
            card.grayCardList[2].value = toThousands(res.data.data.isrelationcustomer)
            card.startDate = res.data.data.startDate
            card.endDate = res.data.data.endDate
            // card.unitText = res.data.data.valueUnit
        }
    }).catch(err => {
        console.log(err)
    });
}
const getChartData = () => {
    dashBoardApi.queryDaptInformationApi({ projectCode: props.biProjectCode, timeType: card.curTab }).then((res) => {
        if (res.data.code == 200) {
            card.tableData = res.data.data
        }
    }).catch(err => {
        console.log(err)
    });
}

const goDetail = (permi) => {
    reportLog({
        type: 4,
        resourceKey: permi,
    })
    router.push({
        path: '/page/routeController/index',
        query: {
            rid: 'Manage/Business/debtInformation/contractArrears',
            rTitle: '欠款追缴合同报表',
            projectCode: props.biProjectCode,
            source: "debt",
			startDate: card.startDate.slice(0, 7),
			endDate: card.endDate.slice(0, 7)
        },
    })
}
const changeTab = (value) => {
    // 切换tab，更换开始和结束日期
    // 按天：开始和结束日期，均为【数据更新日期】
    // 按月：开始日期为【数据更新日期】当月第一天，结束日期为【数据更新日期】
    // 按年：开始日期为【数据更新日期】当年第一天，结束日期为【数据更新日期】
    card.curTab = value
    getCardData()
    getChartData()
}

let tableLabel = ref('楼层')
const getBuilding = () => {
    dashBoardApi.isBuildingsApi({ biProjectCode: props.biProjectCode }).then((res) => {
        if (res.code == 200) {
            tableLabel.value = res.data ? '楼栋' : '楼层'
        }
    }).catch(err => {
        console.log(err)
    });
}
getBuilding()
getCardData()
getChartData()

const biProjectCode = computed(() => {
    return props.biProjectCode
});
watch(biProjectCode, () => {
    getBuilding()
    getCardData()
    getChartData()
}, { deep: true });
</script>

<style scoped lang="scss"></style>
